/* ===========================================================
 * $Id: SecurityServiceImpl.java 23 2009-08-05 06:20:34Z maven.yu $
 * This file is part of Micrite
 * ===========================================================
 *
 * (C) Copyright 2009, by Gaixie.org and Contributors.
 * 
 * Project Info:  http://micrite.gaixie.org/
 *
 * Micrite is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Micrite is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Micrite.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.kingre.et.service.impl;

import java.util.LinkedHashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.ConfigAttributeDefinition;
import org.springframework.security.ConfigAttributeEditor;
import org.springframework.security.intercept.web.RequestKey;
import org.springframework.transaction.annotation.Transactional;

import com.kingre.et.Constants;
import com.kingre.et.dao.IResourceDAO;
import com.kingre.et.model.Resource;
import com.kingre.et.service.ISecurityService;

/**
 * 接口 <code>ISecurityInterceptor</code> 的实现类。
 * 
 */
@Transactional
public class SecurityServiceImpl implements ISecurityService {
    private static final Logger logger = Logger.getLogger(SecurityServiceImpl.class);
    @Autowired
    private IResourceDAO resourceDAO;

    public LinkedHashMap<RequestKey, ConfigAttributeDefinition> loadAuthorities() {
        LinkedHashMap<RequestKey, ConfigAttributeDefinition> requestMap =
            new LinkedHashMap<RequestKey, ConfigAttributeDefinition>();

        List<Resource> Resources = resourceDAO.findByType(Constants.SECURITY_FILTER_URL);

        for (Resource resource : Resources) {
            RequestKey key = new RequestKey(resource.getValue());
            String grantedAuthorities = resource.getRolesString();
            logger.debug("{resource:"+key+";role:"+grantedAuthorities+"}");
            if (grantedAuthorities != null) {
                ConfigAttributeEditor configAttrEditor = new ConfigAttributeEditor();
                configAttrEditor.setAsText(grantedAuthorities);
                ConfigAttributeDefinition definition = (ConfigAttributeDefinition) configAttrEditor.getValue();
                requestMap.put(key, definition);
            }
        }
        return requestMap;
    }

}
